﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CPPEI.Coolzon.Event.Core;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Data;

namespace CPPEI.Coolzon.Theme.Fashion.MainViewControl
{
    public partial class JSControlView
    {
        [Common.Subscribe]
        public void OnOpened(CoreOpenSceneCompletedEvent @event)
        {
            Metadata.Scene openedSceneMetadata = Metadata.SolutionUtil.GetScene(UI.UIContext.Current.Solution, @event.SceneName);
            if (openedSceneMetadata != null)
            {
                if (CZEnvironment.IsShowActionPanelWhenOpenScene() && !IsFreeModel)
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                    CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UI.UIContext.Current.RenderView;
                    renderView.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }

                spRecord.Children.Clear();
                string actionFile = CZEnvironment.GetCustomActionFilePath(UIContext.Current.UserIdentity,
                       UIContext.Current.SolutionName, openedSceneMetadata.Name);
                CPPEI.Coolzon.Metadata.ParserV1.SolutionXmlParser xmlParse = new Metadata.ParserV1.SolutionXmlParser();
                List<CustomAction> customActionList = xmlParse.GetCustomActionList(actionFile);

                SetAction(openedSceneMetadata.ShorcutActions);
                SetAction(openedSceneMetadata.CustomActions);
                SetAction(customActionList);

                if (openedSceneMetadata.IsHorizontalLoopRender)
                {
                    leftLoop.Visibility = System.Windows.Visibility.Visible;
                    rightLoop.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    leftLoop.Visibility = System.Windows.Visibility.Hidden;
                    rightLoop.Visibility = System.Windows.Visibility.Hidden;
                }
                CurrentSceneName = @event.SceneName;

                if (@event.SwitchMode == SwitchMode.Pop)
                {
                    _popedSceneList.Add(@event.SceneName);
                    _ctlBtnClose.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    _popedSceneList.Clear();
                    _ctlBtnClose.Visibility = System.Windows.Visibility.Hidden;
                }
            }
            else
            {
                Common.Logger.ErrorToTag("UI", "快捷按钮面板,场景打开时，在方案中未发现场景:{0}", @event.SceneName);
            }
        }

        /// <summary>
        /// 场景关闭事件
        /// </summary>
        /// <param name="event"></param>
        [Common.Subscribe]
        public void OnHidden(CoreCloseSceneCompletedEvent @event)
        {
            if (@event.SwitchMode == SwitchMode.Pop)
            {
                // 移除场景
                for (int index = 0; index < _popedSceneList.Count; index++)
                {
                    if (_popedSceneList[index] == @event.SceneName)
                    {
                        _popedSceneList.RemoveAt(index);
                    }
                }

                this.Dispatcher.Invoke(() =>
                {
                    if (_popedSceneList.Count > 0)
                    {
                        _ctlBtnClose.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        // 没有场景不显示关闭按钮
                        _ctlBtnClose.Visibility = System.Windows.Visibility.Hidden;
                    }
                });
            }
        }
    }
}
